#include <cmath>
#include "gui/layers/DynamicLayer.h"
#include "gui/util/GUIObjectDrawer.h"
#include "util/LinkedList.h"

namespace gui {

DynamicLayer::DynamicLayer(SurfaceFactory* surfaceFactory): Layer() {
	this->surfaceFactory = surfaceFactory;
}

//void DynamicLayer::updateOffset(int x, int y) {
//	this->offset.x = min(this->offset.x, x);
//	this->offset.y = min(this->offset.y, y);
//}

void DynamicLayer::updateSurface(GUIObjectsContainer &objectsContainer) {

	GUIObjectDrawer drawer(this->surfaceFactory);
	util::LinkedList<GUIObject>::Iterator it = objectsContainer.getGUIObjects().iterator();
	Portion portion;
	portion.x = objectsContainer.getXPos();
	portion.y = objectsContainer.getYPos();
	portion.w = objectsContainer.getWidth();
	portion.h = objectsContainer.getHeight();

	if (this->completeSurface != NULL) delete this->completeSurface;

	this->completeSurface = surfaceFactory->createSurface(portion, BLUE);
	this->completeSurface->setTransparentColor(BLUE);

	while (it.hasNext()) {
		// Dibujo cada objeto restante.
		GUIObject* current = it.next();
		this->completeSurface->applySurface(current->asSurface());
	}
}

}
